﻿Imports System.Data.SqlClient

Public Class Form1

    Public conexion As New SqlConnection("Data Source=.\SQLExpress; Initial Catalog = NuevoZoo;Integrated Security = true")
    Public adapter As New SqlDataAdapter("Select * from Animales", conexion)
    Public midataset As New DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        adapter.Fill(midataset, "Animales")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Animales"

        txtCod.DataBindings.Add("text", midataset, "Animales.Cod")
        txtNombre.DataBindings.Add("text", midataset, "Animales.Nombre")
        txtApellidos.DataBindings.Add("text", midataset, "Animales.Apellidos")
        txtTelefono.DataBindings.Add("text", midataset, "Animales.Telefono")
        TextBox5.DataBindings.Add("text", midataset, "Animales.Observaciones")

    End Sub

    Private Sub Siguiente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Siguiente.Click
        BindingContext(midataset, "Animales").Position += 1
        registro()

    End Sub

    Private Sub Anterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Anterior.Click
        BindingContext(midataset, "Animales").Position -= 1
        registro()

    End Sub

    Private Sub Primero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Primero.Click
        BindingContext(midataset, "Animales").Position = 0
        registro()

    End Sub


    Private Sub Ultimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ultimo.Click
        BindingContext(midataset, "Animales").Position = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Displayed)
        registro()

    End Sub

    Private Sub registro()
        Label6.Text = ((Me.BindingContext(midataset, "Animales").Position + 1).ToString + " de ") + Me.BindingContext(midataset, "Animales").Count.ToString
        If (Me.BindingContext(midataset, "Animales").Position + 1 = Me.BindingContext(midataset, "Animales").Count) Then
            Siguiente.Enabled = False
            Ultimo.Enabled = False
        Else
            Siguiente.Enabled = True
            Ultimo.Enabled = True
        End If

        If (Me.BindingContext(midataset, "Animales").Position = 0) Then
            Primero.Enabled = False
            Anterior.Enabled = False
        Else
            Primero.Enabled = True
            Anterior.Enabled = True
        End If
    End Sub

    Private Sub GuardarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
        Dim comando As SqlCommand
        Dim ordensql As String = "Insert into Animales (Nombre, Apellidos, Telefono, Observaciones) values (@nombre, @apellidos, @telefono, @observaciones)"
        comando = New SqlCommand(ordensql, conexion)
        comando.Parameters.AddWithValue("@nombre", txtNombre.Text)
        comando.Parameters.AddWithValue("@apellidos", txtApellidos.Text)
        comando.Parameters.AddWithValue("@telefono", txtTelefono.Text)
        comando.Parameters.AddWithValue("@observaciones", TextBox5.Text)

        conexion.Open()
        comando.ExecuteNonQuery()
        conexion.Close()
        midataset.Clear()
        adapter.Fill(midataset, "Animales")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Animales"
        registro()


    End Sub

    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        txtNombre.Text = ""
        txtApellidos.Text = ""
        txtTelefono.Text = ""
        txtCod.Text = ""
        TextBox5.Text = ""
        txtNombre.Focus()

    End Sub

    Private Sub ModificarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Dim comando As SqlCommand
        Dim ordensql As String = "Update Animales set Nombre=@nombre, Apellidos=@apellidos, Observaciones=@observaciones where Apellidos=@apellidos"
        comando = New SqlCommand(ordensql, conexion)
        comando.Parameters.AddWithValue("@nombre", txtNombre.Text)
        comando.Parameters.AddWithValue("@apellidos", txtApellidos.Text)
        comando.Parameters.AddWithValue("@telefono", txtTelefono.Text)
        comando.Parameters.AddWithValue("@observaciones", TextBox5.Text)

        conexion.Open()
        comando.ExecuteNonQuery()
        conexion.Close()
        midataset.Clear()
        adapter.Fill(midataset, "Animales")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Animales"
        registro()



    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Dim res As Integer
        Dim eliminar As String = "Delete from Animales where Apellidos=@eli"
        Dim comando As New SqlCommand(eliminar, conexion)
        comando.Parameters.AddWithValue("@eli", txtApellidos.Text)
        res = MsgBox("¿Está seguro de que desea eliminar el registro " & txtCod.Text, 20, "Aviso")
        If res = 6 Then
            conexion.Open()
            comando.ExecuteNonQuery()
            conexion.Close()
            midataset.Clear()
            adapter.Fill(midataset, "Animales")
            DataGridView1.DataSource = midataset
            DataGridView1.DataMember = "Animales"
            registro()
        End If
    End Sub

    Private Sub BuscarToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuscarToolStripMenuItem1.Click
        Dim m As Integer = 0
        Dim pos As Integer = 0
        Dim ordensql As New SqlCommand("Select * from Animales where Apellidos=@apellidos", conexion)
        ordensql.Parameters.AddWithValue("@apellidos", txtApellidos.Text)
        conexion.Open()
        Dim res As SqlDataReader = ordensql.ExecuteReader
        Try
            Do While res.Read
                If res.Item("Apellidos") = txtApellidos.Text Then
                    txtCod.Text = res("Cod")
                    txtNombre.Text = res("Nombre")
                    txtApellidos.Text = res("Apellidos")
                    txtTelefono.Text = res("Telefono")
                    TextBox5.Text = res("Observaciones")
                    m = 1
                Else
                    pos = pos + 1
                End If
            Loop
            If m = 0 Then
                MsgBox("No hay coincidencias")
            End If
        Catch ex As Exception
            MsgBox("Error")
        End Try
        conexion.Close()
    End Sub

    Private Sub BúsquedaAvanzadaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BúsquedaAvanzadaToolStripMenuItem.Click
        Form2.Show()

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class
